/****************************************************************************************************************************
* The original serial port listener by Sun was modified to read and interpret data from a Basic Stamp II. The basic stamp	* 
* has been programmed to allow input from 12 different buttons. The BS2 then sends the data in binary form to the serial 	*
* port. Only 10 inputs are used however, 5 on each hand.																	*
*																															*
* Originaly written by John Hopf																							*
* 																															*
* Editing and name changed by Ross Smith - Jan 2005																			*
* The number of bytes read nust be two, if any other number is read we ignore the results.  Before this was done there would*
* be erratic inaccurate results causing the world to float.																	*
*																															*
****************************************************************************************************************************/

import java.io.*;
import javax.comm.SerialPort;
import javax.comm.SerialPortEventListener;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPortEvent;

public class ButtonDeviceSerialPort implements SerialPortEventListener {
  
	/** buffer to store the read bytes */
	byte[] readBuffer = new byte[200];
   	
	/** Button states */
	private boolean button[] = {false, false, false, false, false,
								false, false, false, false, false};
	
	/** Serial Port Identifiers */
	private InputStream inputStream;
	private SerialPort serialPort;
	static CommPortIdentifier portId;
   
	
	public ButtonDeviceSerialPort(CommPortIdentifier der) {
         
		ButtonDeviceSerialPort.portId = der;
         try {
            /*Set up the serial port*/
         	serialPort = (SerialPort) portId.open("MouseDeviceSerialPortApp", 2000);
            inputStream = serialPort.getInputStream();
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            serialPort.setDTR(false); /*Set to false while not programming basic stamp 2*/       
         } catch (Exception e) {
         	e.printStackTrace();
         }
	}
   
	public void serialEvent(SerialPortEvent event) {
		System.out.println("event");
		switch(event.getEventType()) {
			case SerialPortEvent.DATA_AVAILABLE:    
      			int numBytes = 0;            
               	try {
               		while (inputStream.available() > 0) {
	                    numBytes = inputStream.read(readBuffer,0,2);
	                    
	                    /*Only look at data if the record is 3 bytes long, may ocasionaly be one byte*/
	                    if(numBytes == 2){

	                    	int v = ((int)readBuffer[0] & 0x0FF) + (((int)readBuffer[1] & 0x0FF) << 8); 
	                    	for (int i = 0; i < 10; i++)
	                    		button[i] = ((1 << i) & v) != 0;
	                    }
               		}
               	} 
                catch (IOException e) {
                	e.printStackTrace();
                }            
                break;
           }
      }
	
    /*convert the integer parameter into 16 bit binary*/
	public void toBinary(byte num) {
		for(int i = 7; i >= 0; i--)
			System.out.print((1 << i & num) != 0 ? "1" : "0");
		System.out.println();
    }
   
	private boolean getButton(int id){
		// System.out.println("Button " + id ": " + button[id]);
		return button[id];
	}
	
   	public boolean getButton1() { return getButton(0); }
   	public boolean getButton2() { return getButton(1); }
   	public boolean getButton3() { return getButton(2); }
   	public boolean getButton4() { return getButton(3); }
   	public boolean getButton5() { return getButton(4); }
   	public boolean getButton6() { return getButton(5); }
   	public boolean getButton7() { return getButton(6); }
   	public boolean getButton8() { return getButton(7); }
   	public boolean getButton9() { return getButton(8); }
   	public boolean getButton10() { return getButton(9); }
   
}
